package echecs.vue;

import echecs.launch.ChessInterface;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JApplet;

public abstract class BoardVue extends Canvas implements ChessInterface, MouseListener, MouseMotionListener, Runnable {

    public static int code = 0, //forbid access to the movelist
            start = 21, //index of the start field
            alt = 21, //did the mouse move to an other field?
            end = 21, //index of the end field
            x = 0, //x coordinate
            y = 0;//y coordinate
    public Applet parent;
//    public static int move;//move of the AI

    public BoardVue(JApplet ref) {
        super();
        //initialize alpha beta
        alphabeta[0] = -3000.0f;

//        newgame();

        loadImages(ref);
        //set applet reference
        parent = ref;
        //events
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public void mouseClicked(java.awt.event.MouseEvent e) {
    }

    public void mouseDragged(java.awt.event.MouseEvent e) {
        x = e.getX() / 40;
        if (x < 0) {
            x = 0;
        }
        if (x > 7) {
            x = 7;
        }

        y = e.getY() / 40;
        if (y < 0) {
            y = 0;
        }
        if (y > 7) {
            y = 7;
        }

        end = 21 + y * 10 + x;

        if (end != alt) {
            //rebuild old field
            if (alt != start) {
                paintSquare(alt);
            }

            if (end != start) {	//mark new field
                Graphics g = getGraphics();

                if ((code != 1) && (ishHumanMoveValid(start * 100 + end))) {
                    g.setColor(green);
                } else {
                    g.setColor(red);
                }

                g.fillRect(x * 40, y * 40, 40, 40);
                try {
                    g.drawImage(pieces[graphboard[end] % 100 - 10], x * 40, y * 40, 40, 40, parent);
                } catch (ArrayIndexOutOfBoundsException ex) {
                }
            }

            alt = end;
        }
    }

    public void mouseEntered(java.awt.event.MouseEvent e) {
    }

    public void mouseExited(java.awt.event.MouseEvent e) {
    }

    public void mouseMoved(java.awt.event.MouseEvent e) {
    }

    public void mousePressed(java.awt.event.MouseEvent e) {
        x = e.getX() / 40;
        if (x < 0) {
            x = 0;
        }
        if (x > 7) {
            x = 7;
        }

        y = e.getY() / 40;
        if (y < 0) {
            y = 0;
        }
        if (y > 7) {
            y = 7;
        }

        start = 21 + y * 10 + x;
        alt = start;
        end = start;

        //mark start field
        Graphics g = getGraphics();
        g.setColor(blue);
        g.fillRect(x * 40, y * 40, 40, 40);
        try {
            g.drawImage(pieces[graphboard[start] % 100 - 10], x * 40, y * 40, 40, 40, parent);
        } catch (ArrayIndexOutOfBoundsException ex) {
        }
    }

    public void mouseReleased(MouseEvent e) {
        //erase marks
        paintSquare(start);
        paintSquare(end);

        //execute move if valid
        if ((code != 1) && (ishHumanMoveValid(start * 100 + end))) {
            execute(start, end);
        }
    }

    //paint chessboard
    @Override
    public void paint(Graphics g) {
        for (int i = 21; i < 99; i++) {
            paintSquare(i);
            if (i % 10 == 8) {
                i += 2;
            }
        }
    }
    //paint single field

    public void paintSquare(int index) {
        //load graphic reference
        Graphics g = getGraphics();

        //calculate x and y
        int xx = (index - 21) % 10;
        int yy = (index - 21) / 10;

        //paint ground field
        if ((xx * 11 + yy) % 2 == 0) {
            g.setColor(hell);
        } else {
            g.setColor(dunkel);
        }

        g.fillRect(xx * 40, yy * 40, 40, 40);

        //paint piece
        try {
            g.drawImage(pieces[graphboard[index] % 100 - 10], xx * 40, yy * 40, 40, 40, parent);
        } catch (ArrayIndexOutOfBoundsException e) {
        }
    }

    private void loadImages(JApplet ref) {
        //load images
        MediaTracker controler = new MediaTracker(ref);
        pieces[1] = ref.getImage(ref.getCodeBase(), "images/white_pawn.gif");
        pieces[2] = ref.getImage(ref.getCodeBase(), "images/white_knight.gif");
        pieces[3] = ref.getImage(ref.getCodeBase(), "images/white_bishop.gif");
        pieces[4] = ref.getImage(ref.getCodeBase(), "images/white_rook.gif");
        pieces[5] = ref.getImage(ref.getCodeBase(), "images/white_queen.gif");
        pieces[6] = ref.getImage(ref.getCodeBase(), "images/white_king.gif");
        pieces[11] = ref.getImage(ref.getCodeBase(), "images/black_pawn.gif");
        pieces[12] = ref.getImage(ref.getCodeBase(), "images/black_knight.gif");
        pieces[13] = ref.getImage(ref.getCodeBase(), "images/black_bishop.gif");
        pieces[14] = ref.getImage(ref.getCodeBase(), "images/black_rook.gif");
        pieces[15] = ref.getImage(ref.getCodeBase(), "images/black_queen.gif");
        pieces[16] = ref.getImage(ref.getCodeBase(), "images/black_king.gif");
        for (int i = 1; i < 7; i++) {
            controler.addImage(pieces[i], 0);
            controler.addImage(pieces[i + 10], 0);
        }
        try {
            controler.waitForAll(); //wait until the images are loaded
        } catch (InterruptedException e) {
            System.out.println("Images not successfull loaded - Trying again ...");
            controler.checkID(0, true);
        }
    }
}
